package cn.itcast.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.abel533.entity.Example;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.itcast.mapper.NewUserMapper;
import cn.itcast.mapper.UserMapper;
import cn.itcast.pojo.EasyUIResult;
import cn.itcast.pojo.User;
import cn.itcast.service.UserService;
@Service
public class UserServiceImpl implements UserService {
	@Autowired
	private  NewUserMapper userMapper;
	@Override
	public EasyUIResult queryEasyUIResult(Integer page, Integer rows) {
		//查询总数
		//Long count = userMapper.queryUserCount();
		/*//查询用户
		List<User> users = useMapper.queryUserList();*/
		//每次开始的下标
		/*int firstPage = (page - 1)*rows;
		//每页的数据量
		int pageNum = rows;
		List<User> users = userMapper.queryUserPage(firstPage,pageNum);*/
		//调用分页插件的静态方法
		PageHelper.startPage(page, rows);
		//查询用户
		List<User> users = userMapper.select(null);
		PageInfo<User> pageInfo = new PageInfo<User>(users);
		//把数据封装到对象中
		EasyUIResult result = new EasyUIResult();
		result.setTotal(pageInfo.getTotal());
		result.setRows(pageInfo.getList());
		return result;
	}
	@Override
	public Boolean addUser(User user) {
		Integer count = userMapper.insert(user);
		if(count == 1){
			return true;
		}
		return false;
	}
	@Override
	public Boolean deleteUserById(Long[] ids) {
		/*int count = userMapper.deleteUserById(ids);*/
	Example example = new Example(User.class);
		
		List<Object> list = new ArrayList<Object>();
		for (Long id : ids) {
			list.add(id);
		}
		example.createCriteria().andIn("id", list);
		int count = userMapper.deleteByExample(example);
		if(count>=1){
			return true;
		}
		return false;
	}
	@Override
	public Boolean editUser(User user) {
		Integer count = userMapper.updateByPrimaryKeySelective(user);
		if(count == 1){
			return true;
		}
		return false;
	}

}
